package com.hdwx.weike.controllers;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hdwx.weike.beans.PageBean;
import com.hdwx.weike.service.IHotSearchService;
import com.hdwx.weike.service.IVideoService;
import com.hdwx.weike.service.impl.HotSearchServiceImpl;
import com.hdwx.weike.service.impl.VideoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by hdwx on 2017/1/5.
 */
@WebServlet(urlPatterns = "/hotSearch.do")
public class HotSearchController extends BaseController{
    IHotSearchService hotSearchService=new HotSearchServiceImpl();
    IVideoService videoService=new VideoServiceImpl();
    @Override
    public void todo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入热搜服务");
        String name = req.getParameter("keyName");
        String index=req.getParameter("index");
        String pid=req.getParameter("sortId");
        System.out.println(name);
        if(name!=null&&name!=""){
            //处理搜索的关键字
            hotSearchService.dealSearchKey(name);
            //通过关键字搜索视频
            int pageIndex=1; //默认第一页
            int sortId=0;//默认排序
            if(index!=null){
                try {
                    pageIndex=Integer.valueOf(index);
                }catch (Exception e){}
            }
            if(pid!=null){
                try {
                    sortId=Integer.valueOf(pid);
                }catch (Exception e){}
            }
            PageBean pageBean = videoService.queryByName(name, pageIndex, sortId);
            String jsonPage = JSON.toJSONString(pageBean);
            System.out.println(jsonPage);
            resp.getWriter().print(jsonPage);
        }
    }
}
